package com.amado.course37CharStream.homework;

import java.io.*;

/**
 * Created by Administrator on 2018/8/3.
 */
public class MergeFile {
    public static void main(String[] args) throws IOException {
        File srcOne = new File("D:\\code\\gitee\\javase\\src\\main\\java\\com\\amado\\course37CharStream\\homework\\a.txt");
        File srcTwo = new File("D:\\code\\gitee\\javase\\src\\main\\java\\com\\amado\\course37CharStream\\homework\\b.txt");
        File targetFile = new File("D:\\code\\gitee\\javase\\src\\main\\java\\com\\amado\\course37CharStream\\homework\\c.txt");
        readByInputStream(srcOne,srcTwo,targetFile);
//        readByWriter(srcOne,srcTwo,targetFile);

    }
    public static void readByInputStream(File srcOne,File srcTwo,File targetFile){
        InputStream oneReader =null;
        InputStream twoReader = null;
        OutputStream writer = null;
        try {
            oneReader = new FileInputStream(srcOne);
            twoReader = new FileInputStream(srcTwo);
            writer = new FileOutputStream(targetFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] charArray = new byte[100];
        int length;
        try {
            while((length=oneReader.read(charArray))!=-1){
                System.out.println("length" +length);
                writer.write(charArray,0,length);
            }
            //换行10
            writer.write(10);
            while((length=twoReader.read(charArray))!=-1){
                System.out.println("length2" +length);
                writer.write(charArray,0,length);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            oneReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            twoReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void readByWriter(File srcOne,File srcTwo,File targetFile) {
        Reader oneReader = null;
        Writer writer = null;
        Reader twoReader = null;
        try {
            oneReader = new FileReader(srcOne);
            twoReader = new FileReader(srcTwo);
            writer = new FileWriter(targetFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        char[] charArray = new char[100];
        int length;
        try {
            while((length=oneReader.read(charArray))!=-1){
                System.out.println("length" +length);
                writer.write(charArray,0,length);
            }
            writer.write("\n");
            while((length=twoReader.read(charArray))!=-1){
                System.out.println("length2" +length);
                writer.write(charArray,0,length);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            oneReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            twoReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
